/**
 * 
 */
package com.china08.yxyapi.service.manage.edu.platform.statistics.impl;

import java.sql.Time;
import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.db.attendance.YxyYxyTecAttendanceConfig;
import com.china08.yxyapi.entity.db.yxy.YxyHoliday;
import com.china08.yxyapi.entity.mg.attendance.YxyTeaAttendance;
import com.china08.yxyapi.entity.mg.attendance.YxyTeaAttendanceStatistics;
import com.china08.yxyapi.rabbitmq.listener.TecAttendanceStatisticsListener.TeaRespModel;
import com.china08.yxyapi.repository.mg.attendance.YxyTeaAttendanceStatisticsRepository;
import com.china08.yxyapi.service.manage.edu.platform.statistics.ITeaAttendenceStatisticsService;
import com.china08.yxyapi.util.DateUtils;

@Service
public class TeaAttendanceStatisticsService implements ITeaAttendenceStatisticsService {

    @Autowired
    private YxyTeaAttendanceStatisticsRepository yxyTeaAttendanceStatisticsRepository;

    public void switchTimes(String times, TeaRespModel teacher, List<YxyTeaAttendance> attendanceList,
            YxyYxyTecAttendanceConfig config, Integer day, List<YxyHoliday> holiday) {
        switch (times) {
        case "2":
            twoTimesOfDay(teacher, attendanceList, config, day, holiday);
            break;
        case "4":
            fourTimesOfDay(teacher, attendanceList, config, day, holiday);
            break;
        default:
            break;
        }
    }

    /**
     * 两次打卡的统计
     * 
     */
    @Override
    public void twoTimesOfDay(TeaRespModel teacher, List<YxyTeaAttendance> attendanceList,
            YxyYxyTecAttendanceConfig config, Integer day, List<YxyHoliday> holiday) {
        Time time1 = config.getClock1();// 打卡第一次时间
        Time time2 = config.getClock2();// 打卡第二次时间
        Time time15 = getMedialTime(config.getClock2(), config.getClock1());// 打卡在第一次和第二次之间时间

        YxyTeaAttendanceStatistics entity = new YxyTeaAttendanceStatistics();
        entity.setId(teacher.getTeacherId() + "_" + teacher.getSchoolId() + "_" + day);
        entity.setSchoolId(teacher.getSchoolId());
        entity.setTeacherId(teacher.getTeacherId());
        entity.setTeacherName(teacher.getTeacherNick());
        entity.setSchoolName(teacher.getSchoolNick());
        entity.setDayOfDate(day);
        entity.setTimes("2");
        String[] isholi = isHoliday(day, holiday, config);
        if (isholi[0].equals("1")) {
            entity.setIsHoliday(1);
            entity.setHolidayDescription(isholi[1]);
        } else {
            entity.setIsHoliday(0);
        }
        try {
            entity.setWeekOfDate(DateUtils.getWeek1(DateUtils.formatStrDate4(day)));
            entity.setDate(DateUtils.formatStrDate4(day));
        } catch (ParseException e) {
            entity.setWeekOfDate("");
            e.printStackTrace();
        }
        if (attendanceList != null && attendanceList.size() > 0) {
            // 根据设定的打卡时间,比较打卡是否正常
            List<YxyTeaAttendance> time1Att = attendanceList.stream()
                    .filter(att -> DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time1))
                    .collect(Collectors.toList());
            if (time1Att.size() > 0) {
                // 如果有小于第一次打卡时间的. 第一次打卡时间正常
                entity.setSignStatus1(1);
                entity.setSignTime1(time1Att.get(0).getSignTime());
            } else {
                // 如果没有查找第一次和第二次中间时间是否有.
                List<YxyTeaAttendance> time15Att = attendanceList.stream()
                        .filter(att -> DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time15))
                        .collect(Collectors.toList());
                if (time15Att.size() > 0) {// 如果存在 ,代表迟到
                    entity.setSignStatus1(2);
                    // 获取多次打卡时间的 第一次打卡时间, 迟到按照最先打卡时间.
                    entity.setSignTime1(time15Att.get(0).getSignTime());
                } else {
                    entity.setSignStatus1(0);// 如果不存在, 代表没打卡
                    entity.setSignTime1(null);
                }
            }

            // 计算第二次打卡
            List<YxyTeaAttendance> time2Att = attendanceList.stream()
                    .filter(att -> DateUtils.greaterEqualCompareDate(DateUtils.getTime(att.getSignTime()), time2))
                    .collect(Collectors.toList());
            if (time2Att.size() > 0) {
                entity.setSignStatus2(1);
                entity.setSignTime2(time2Att.get(time2Att.size() - 1).getSignTime());
            } else {
                // 如果没有查找第一次和第二次中间时间是否有.
                List<YxyTeaAttendance> time25Att = attendanceList.stream()
                        .filter(att -> DateUtils.greaterCompareDate(DateUtils.getTime(att.getSignTime()), time15)
                                && DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time2))
                        .collect(Collectors.toList());
                if (time25Att.size() > 0) {// 如果存在 ,代表早退
                    entity.setSignStatus2(3);
                    entity.setSignTime2(time25Att.get(time25Att.size() - 1).getSignTime());
                } else {
                    entity.setSignStatus2(0);// 如果不存在, 代表没打卡
                    entity.setSignTime2(null);
                }
            }

        } else {
            // 今天未打卡, 记录全天未打卡
            entity.setSignTime1(null);
            entity.setSignStatus1(0);
            entity.setSignTime2(null);
            entity.setSignStatus2(0);
        }
        yxyTeaAttendanceStatisticsRepository.save(entity);

    }

    /**
     * 四次打卡记录次打卡的统计
     * 
     */
    @Override
    public void fourTimesOfDay(TeaRespModel teacher, List<YxyTeaAttendance> attendanceList,
            YxyYxyTecAttendanceConfig config, Integer day, List<YxyHoliday> holiday) {

        Time time1 = config.getClock1();// 打卡第一次时间
        Time time2 = config.getClock2();// 打卡第二次时间
        Time time15 = getMedialTime(config.getClock2(), config.getClock1());// 打卡在第一次和第二次之间时间
        Time time3 = config.getClock3();// 打卡第三次时间
        Time time25 = getMedialTime(config.getClock3(), config.getClock2());// 打卡在第二和第三次之间的时间
        Time time4 = config.getClock4();// 打卡第四次时间
        Time time35 = getMedialTime(config.getClock4(), config.getClock3());// 打卡在第三次和第四次之间的时间

        YxyTeaAttendanceStatistics entity = new YxyTeaAttendanceStatistics();
        entity.setId(teacher.getTeacherId() + "_" + teacher.getSchoolId() + "_" + day);

        entity.setSchoolId(teacher.getSchoolId());
        entity.setTeacherId(teacher.getTeacherId());
        entity.setTeacherName(teacher.getTeacherNick());
        entity.setSchoolName(teacher.getSchoolNick());
        entity.setDayOfDate(day);
        entity.setTimes("4");
        try {
            entity.setWeekOfDate(DateUtils.getWeek1(DateUtils.formatStrDate4(day)));
            entity.setDate(DateUtils.formatStrDate4(day));
        } catch (ParseException e) {
            entity.setWeekOfDate("");
            e.printStackTrace();
        }
        String[] isholi = isHoliday(day, holiday, config);
        if (isholi[0].equals("1")) {
            entity.setIsHoliday(1);
            entity.setHolidayDescription(isholi[1]);
        } else {
            entity.setIsHoliday(0);
        }
        if (attendanceList != null && attendanceList.size() > 0) {
            // 根据设定的打卡时间,比较打卡是否正常
            List<YxyTeaAttendance> time1Att = attendanceList.stream()
                    .filter(att -> DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time1))
                    .collect(Collectors.toList());
            if (time1Att.size() > 0) {
                // 如果有小于第一次打卡时间的. 第一次打卡时间正常
                entity.setSignStatus1(1);
                entity.setSignTime1(time1Att.get(0).getSignTime());
            } else {
                // 如果没有查找第一次和第二次中间时间是否有.
                List<YxyTeaAttendance> time15Att = attendanceList.stream()
                        .filter(att -> DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time15))
                        .collect(Collectors.toList());
                if (time15Att.size() > 0) {// 如果存在 ,代表迟到
                    // 如果有小于第一次打卡时间的. 第一次打卡时间正常
                    entity.setSignStatus1(2);
                    entity.setSignTime1(time15Att.get(0).getSignTime());
                } else {
                    entity.setSignStatus1(0);// 如果不存在, 代表没打卡
                    entity.setSignTime1(null);
                }
            }

            // 计算第二次打卡
            List<YxyTeaAttendance> time2Att = attendanceList.stream()
                    .filter(att -> DateUtils.greaterEqualCompareDate(DateUtils.getTime(att.getSignTime()), time2)
                            && DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time25))
                    .collect(Collectors.toList());
            if (time2Att.size() > 0) {
                entity.setSignStatus2(1);
                entity.setSignTime2(time2Att.get(0).getSignTime());
            } else {
                // 如果没有查找第一次和第二次中间时间是否有.
                List<YxyTeaAttendance> time25Att = attendanceList.stream()
                        .filter(att -> DateUtils.greaterCompareDate(DateUtils.getTime(att.getSignTime()), time15)
                                && DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time2))
                        .collect(Collectors.toList());
                if (time25Att.size() > 0) {// 如果存在 ,代表早退
                    entity.setSignStatus2(3);
                    entity.setSignTime2(time25Att.get(time25Att.size() - 1).getSignTime());
                } else {
                    entity.setSignStatus2(0);// 如果不存在, 代表没打卡
                    entity.setSignTime2(null);
                }
            }
            // 计算第三次打卡
            List<YxyTeaAttendance> time3Att = attendanceList.stream()
                    .filter(att -> DateUtils.greaterCompareDate(DateUtils.getTime(att.getSignTime()), time25)
                            && DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time3))
                    .collect(Collectors.toList());
            if (time3Att.size() > 0) {
                // 如果有小于第一次打卡时间的. 第一次打卡时间正常
                entity.setSignStatus3(1);
                entity.setSignTime3(time3Att.get(0).getSignTime());
            } else {
                // 如果没有查找第一次和第二次中间时间是否有.
                List<YxyTeaAttendance> time35Att = attendanceList.stream()
                        .filter(att -> DateUtils.greaterEqualCompareDate(DateUtils.getTime(att.getSignTime()), time3)
                                && DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time35))
                        .collect(Collectors.toList());
                if (time35Att.size() > 0) {// 如果存在 ,代表迟到
                    entity.setSignStatus3(2);
                    entity.setSignTime3(time35Att.get(0).getSignTime());
                } else {
                    entity.setSignStatus3(0);// 如果不存在, 代表没打卡
                    entity.setSignTime3(null);
                }
            }

            // 计算第四次打卡
            List<YxyTeaAttendance> time4Att = attendanceList.stream()
                    .filter(att -> DateUtils.greaterEqualCompareDate(DateUtils.getTime(att.getSignTime()), time4))
                    .collect(Collectors.toList());
            if (time4Att.size() > 0) {
                // 如果有小于第一次打卡时间的. 第一次打卡时间正常
                entity.setSignStatus4(1);
                entity.setSignTime4(time4Att.get(time4Att.size() - 1).getSignTime());
            } else {
                // 如果没有查找第一次和第二次中间时间是否有.
                List<YxyTeaAttendance> time35Att = attendanceList.stream()
                        .filter(att -> DateUtils.greaterCompareDate(DateUtils.getTime(att.getSignTime()), time35)
                                && DateUtils.lessEqualCompareDate(DateUtils.getTime(att.getSignTime()), time4))
                        .collect(Collectors.toList());
                if (time35Att.size() > 0) {// 如果存在 ,代表早退
                    entity.setSignStatus4(3);
                    entity.setSignTime4(time35Att.get(time35Att.size() - 1).getSignTime());
                } else {
                    entity.setSignStatus4(0);// 如果不存在, 代表没打卡
                    entity.setSignTime4(null);
                }
            }

        } else {
            // 今天未打卡, 记录全天未打卡
            entity.setSignTime1(null);
            entity.setSignStatus1(0);
            entity.setSignTime2(null);
            entity.setSignStatus2(0);
            entity.setSignTime3(null);
            entity.setSignStatus3(0);
            entity.setSignTime4(null);
            entity.setSignStatus4(0);

        }
        yxyTeaAttendanceStatisticsRepository.save(entity);
    }

    private Time getMedialTime(Time time1, Time time2) {
        return new Time(time1.getTime() + (time2.getTime() - time1.getTime()) / 2);
    }

    private String[] isHoliday(Integer date, List<YxyHoliday> yxyHoliday, YxyYxyTecAttendanceConfig config) {
        String[] holiday = { "0", "假日" };
        List<YxyHoliday> li1 = yxyHoliday.stream().filter(hli -> DateUtils.formatDate4(hli.getDay()) == date)
                .collect(Collectors.toList());

        if (li1 != null && li1.size() > 0) {
            holiday[0] = "1";
            holiday[1] = li1.get(0).getDescription();
            return holiday;
        }

        if (config.getSummerVacationBegin() != null && config.getSummerVacationEnd() != null
                && DateUtils.formatDate4(config.getSummerVacationBegin()) <= date
                && date <= DateUtils.formatDate4(config.getSummerVacationEnd())) {
            holiday[0] = "1";
            holiday[1] = "暑假";
            return holiday;
        }
        if (config.getWinterVacationBegin() != null && config.getWinterVacationEnd() != null
                && DateUtils.formatDate4(config.getWinterVacationBegin()) <= date
                && date <= DateUtils.formatDate4(config.getWinterVacationEnd())) {
            holiday[0] = "1";
            holiday[1] = "寒假";
            return holiday;
        }

        try {
            switch (DateUtils.getWeek2(DateUtils.formatStrDate4(date))) {
            case 2:
                holiday[0] = config.getMonday() ? "0" : "1";
                break;
            case 3:
                holiday[0] = config.getTuesday() ? "0" : "1";
                break;
            case 4:
                holiday[0] = config.getWednesday() ? "0" : "1";
                break;
            case 5:
                holiday[0] = config.getThursday() ? "0" : "1";
                break;
            case 6:
                holiday[0] = config.getFriday() ? "0" : "1";
                break;
            case 7:
                holiday[0] = config.getSaturday() ? "0" : "1";
                break;
            case 1:
                holiday[0] = config.getSunday() ? "0" : "1";
                break;
            default:
            }

        } catch (ParseException e) {
        }

        return holiday;
    }

}
